package cn.net.xyan.easy.webapp.base.controller;

import cn.net.xyan.easy.spring.web.annotation.RestServiceConfig;
import cn.net.xyan.easy.spring.web.config.MVCApplicationContextConfig;
import cn.net.xyan.easy.webapp.base.annotation.CurrentUser;
import cn.net.xyan.easy.webapp.base.api.dto.QueryUserRsq;
import cn.net.xyan.easy.webapp.base.api.dto.QueryWebModuleRsq;
import cn.net.xyan.easy.webapp.base.entity.SystemUser;
import cn.net.xyan.easy.webapp.base.service.IDTOService;
import cn.net.xyan.easy.webapp.base.service.ISecurityService;
import cn.net.xyan.easy.webapp.base.service.IWebModuleService;
import cn.net.xyan.easy.webapp.base.support.SecurityUserDetails;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.webjars.RequireJS;
import org.webjars.WebJarAssetLocator;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by xiashenpin on 16/2/1.
 */
@Controller
@RequestMapping("/")
@RestServiceConfig(packageName = "cn.net.xyan.easy.webapp.base.frontend.service",typeName = "SystemRootAsync")
public class RootController {

    @Autowired
    IWebModuleService webModuleService;

    @Autowired
    ISecurityService securityService;

    @Autowired
    IDTOService dtoService;

    @RequestMapping(value = "/",produces = MediaType.TEXT_HTML_VALUE)
    public String indexPage(ModelMap mm){
        return "default/index";
    }


    @RequestMapping(value = "/context",produces = "application/javascript")
    public String contextJS(ModelMap mm){
        Map<String, String> webJars = new WebJarAssetLocator().getWebJars();

        Map<String, ObjectNode> webjarjson = RequireJS.getSetupJson(ControllerAdvice.contextPath()+MVCApplicationContextConfig.WEBJARROOT);
        mm.addAttribute("webJars",webJars.entrySet());

        List<String> modules = new LinkedList<>();

        for (ObjectNode node: webjarjson.values()){
            if (node!=null){
                String m = node.toString();
                if (m.length()>0)
                    modules.add(m);
            }
        }

        mm.addAttribute("json",modules);

        mm.addAttribute("ContentType","application/javascript");

        mm.addAttribute("restServices",webModuleService.allRestService());

        return "dependence";
    }

    @RequestMapping(value = "/login",produces = MediaType.TEXT_HTML_VALUE)
    public String loginPage(ModelMap mm){
        return "default/sys/login";
    }

    @RequestMapping(value = "/modules",produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<QueryWebModuleRsq> queryModules(@CurrentUser SecurityUserDetails user){
        return webModuleService.queryModuleForUser(user.getUser());
    }

    @RequestMapping(value = "/currentUser",produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    QueryUserRsq queryCurrentUser(@CurrentUser SecurityUserDetails user){
        SystemUser systemUser = securityService.queryUserByUsername(user.getUsername());
        return dtoService.convert(QueryUserRsq.class,systemUser);
    }


}
